function load(){
	search();
}


///分页栏
function gotopage(i) {
	$('#checkPage').attr('checked',"");
	showLoading();
	// 纯数字正则
	var numRule = /(^\d*$)|(^\S+\s+\S+$)/;
	var j = 1;
	var maxPage = parseInt($("#allpage").html());
	// 首页
	if (i == 0) {
		j = 1;
	}
	// 下一页
	else if (i == 1) {
		j = parseInt($("#curpage").html()) + 1;
	}
	// 上一页
	else if (i == 2) {
		j = parseInt($("#curpage").html()) - 1;
	}
	// 跳转
	else if (i == 3) {
		var gotoPage = $("#txtpages").val().trim();
		if (gotoPage == "") {
			j = 1;
		} else if (!gotoPage.match(numRule)) {
			j == 1
		} else {
			j = parseInt(gotoPage);
		}
	}
	// 尾页
	else if (i == -1) {
		j = maxPage;
	}
	if (j < 1) {
		j = 1;
	}
	if (j > maxPage) {
		j = maxPage;
	}
	$("#gotoPage").val(j);
	var update = $('#leftFY').val();
	if("update"==update){
		updateMappingData();
	}else{
		search();
	}
}

function makeThisPage(){
	var flag = true;
	$('.checkState').each(function(){
		var operaTd = $(this).parent().prev();
		var operaInput = $(operaTd).children()[0];
		var operatorType = $(operaInput).val();
		if("insert"==operatorType){
			flag=false;
			$('#checkPage').attr('checked','');
			return;
		}
	});
	
	if(!flag){
		alert("还有映射未添加，请先添加映射！");
	}else{
		showLoading();
		var idsStr = "";
		var checked = $('#checkPage').attr("checked");
		if(checked){
			state = 1;
		}else{
			state = 0;
		}
		$('.checkState').each(function(){
			var tr = $(this).parent().parent();
			var tds = $(tr).children();
			var id = $(tds[0]).text().trim();
			idsStr += id +",";
		});
		idsStr = idsStr.substring(0,idsStr.length-1);
		$.ajax({
			url: "makeAPage",
			type: "GET",
			contentType: "application/json",
			dataType: "json",
			async:false,
			data:{
				idsStr : idsStr,
				state : state
			},
			success:function(){
				alert("使能映射成功!");
				window.location.reload();
			},
			error:function(){
				$('#checkPage').attr('checked','');
				alert("使能映射失败!");
			}
		})
	}
}

function makeAll(){

	showLoading();
	var square = $('#square').val();
	var type = $('#type').val();
	$.ajax({
		url:"makeAllFiltered",
		type:"post",
		dataType:"json",
		async:true,
        data: { //发送给数据库的数据
		 	square : square,
		 	type : type
		 },
		success:function(msg){
			 alert("使能成功");
			 search();
		 },
		error: function (XMLHttpRequest, textStatus, thrownError) {
			 flag = false;
		 }
	
	});

}

function makeOne(element){
	var checked = $(element).attr('checked');
	var operatorTd = $(element).parent().prev();
	var input = $(operatorTd).children()[0];
	if($(input).is('input')){
		var operatorType = $(input).val();
		if("update"==operatorType){
			var $temp = $(operatorTd).parent();
			while(!$temp.is('tr')){
				$temp = $temp.parent();
			}
			var $tr = $temp;
			var type = $('#type').val();
			var array = $tr.children();
			var id = $(array[0]).text()
			var innerId = $(array[2]).text();
			var innerName = $(array[1]).text();
			var outId = $($(array[4]).children()[0]).val();
			var outName = $($(array[3]).children()[0]).val();
			var operation = $($(array[5]).children()[0]).val();
			var state = $(array[6]).children()[0].checked;
			var objectType = $($(array[7]).children()[0]).val();
			state = state ? 1 : 0;
			$.ajax({
				url:"saveOrUpdate",
				type:"post",
				dataType:"json",
				async:true,
		        data: { //发送给数据库的数据
					id : id,
				 	type : type,
				 	innerId : innerId,
				 	innerName : innerName,
				 	outId : outId,
				 	outName : outName,
				 	operation : operation,
				 	objectType : objectType,
				 	state : state
				 },
				success:function(msg){
					 alert("使能操作成功");
					 if(checked){
						 var outIdTd = $(operatorTd).prev();
						 var outNameTd = $(outIdTd).prev();
						 var nameInput = $(outNameTd).children()[0];
						 var idInput = $(outIdTd).children()[0];
						 var outN = $(nameInput).val();
						 var outI = $(idInput).val();
						 $(outNameTd).children().remove();
						 $(outNameTd).text(outN);
						 $(outIdTd).children().remove();
						 $(outIdTd).text(outI);
					 }else{
						 var outIdTd = $(operatorTd).prev();
						 var outNameTd = $(outIdTd).prev();
						 var outN = $(outNameTd).text();
						 var outI = $(outIdTd).text();
						 var optionN = "<input type='text'  style='width:12em' value='"+outN+"'></input>";
						 var optionI = "<input type='text'  style='width:12em' value='"+outI+"'></input>";
						 $(outNameTd).text("").append(optionN);
						 $(outIdTd).text("").append(optionI);
					 }
					 updateCheckPage();
				 },
				error: function (XMLHttpRequest, textStatus, thrownError) {
					 alert("使能操作失败")
				 }
			
			});
		}else{
			$(element).attr('checked','');
			alert("请先添加映射!");
		}
	}
}

function search() {
	showLoading();
	var currentPage = $('#gotoPage').val();
	var type = $('#type').val();
	var objectType = $('#objectType').val();
	var state = $('#state').val();
	var square = $('#square').val();
	var innerName = $('#innerName').val();
	var checkAll = $('#checkAll').attr('checked');
	var checkPage = $('#checkPage').attr('checked');
	$.ajax( {
		type : "GET",
		contentType : "application/json",
		data : {
			currentPage : currentPage,
			type : type,
			objectType : objectType,
			square : square,
			innerName : encodeURI(innerName),
			state : state
		},
		dataType : 'json',
		url : "ajaxMappingDataList",
		async : true,
		success : function(msgs) {
			$('#leftFY').show();
			$('#rightFY').show();
			$('#myTable tr:not(:first)').empty();
			var mappingDataList = msgs.mappingDataList;
			
			$('#curpage').html(msgs.currentPage);
			$('#allpage').html(msgs.maxPage);
			if (msgs.maxPage == 1) {
				$('#leftFY').hide();
				$('#rightFY').hide();
			}
			if (msgs.currentPage == 1) {
				$('#leftFY').hide();
			}
			if (msgs.currentPage == msgs.maxPage) {
				$('#rightFY').hide();
			}
			if(mappingDataList.length==0){
				$('#divfanye').hide();
			}else{
				$('#divfanye').show();
			}
			// 获取语言包
			var language = msgs.language;
			// 获取当前登录用户名
			var recentUserName = msgs.recentUserName;
			if(mappingDataList.length==0){
				$('#myTable')
				.append("<tr><td class='xytd' colspan=10>没有您要查询的数据！</tr>");
			}else{
				for(var i=0;i<mappingDataList.length;i++){

					var tempMappingData = mappingDataList[i];
					var id = tempMappingData.id;
					var innerId = tempMappingData.innerid;
					var innerName = tempMappingData.innername;
					var outId = tempMappingData.outid;
					var outName = tempMappingData.outname;
					var state = tempMappingData.state;
					var operation = tempMappingData.operation;
					var objectType = tempMappingData.objectType;
					
					var hiddenOperation = "<input type='hidden' value='"+operation+"'/>";
					var hiddenObjectType = "<input type='hidden' value='"+objectType+"'/>";
					
					var checkbox = "";
					var outN = "";
					var outI = "";
					if(state == "1"){
						checkbox = "<input type='checkbox' class='checkState' style='margin-right:0.2em' checked='checked'>使能</input>";
						outN = outName;
						outI = outId;
					}else{
						checkbox = "<input type='checkbox' class='checkState' style='margin-right:0.2em'>使能</input>"
						outN = "<input type='text'  style='width:12em' value='"+outName+"'></input>";
						outI = "<input type='text'  style='width:12em' value='"+outId+"'></input>";
					}
					
					var save = "<a href='#' class='fontOrange saveClass' style='color:#ee7800 !import'" 
								+ "'<span>"+"添加"+"</span> </a>";
					
					var del = "<a href='#' class='fontOrange delClass' style='color:#ee7800 !import'" 
						+ "'<span>"+"删除"+"</span> </a>";
					
					var op;
					if("insert"==operation){
						op=save;
					}else{
						op=del;
					}
					
					$('#myTable')
							.append(
									"<tr>"
											+ "<td style='display:none' class='mappingDataId'>"
											+ id
											+ "</td><td>"
											+ innerName
											+ "</td><td>"
											+ innerId
											+ "</td><td>"
											+ outN
											+ "</td><td>"
											+ outI
											+ "</td><td style='display:none;width:0px'>"
											+ hiddenOperation
											+ "</td><td>"
											+ checkbox
											+ "</td><td style='display:none;width:0px'>"
											+ hiddenObjectType
											+ "</td><td><div class='editdiv' value='"+tempMappingData.id+"'>"
											+ op
											+ "</div></td></tr>");
				
				}
			}
			hideLoading();
			addClickEventForSave();
			addClickEventForCheckBox();
			addClickEventForDel();
			updateCheckPage();
		},
		error : function(mm, a, b, c, d, e) {
			hideLoading();
			myAlert("提示", "服务器好像开小差了，刷新一下试试吧！");
		}
	});

}


function updateMappingData(){
	showLoading();
	var currentPage = $('#gotoPage').val();
	var objectType = $('#objectType').val();
	var parentType = $('#parentType').val();
	var parentNameId = $('#parentName').val();
	var state = $('#state').val();
	var type = $('#type').val();
	var innerName = $('#innerName').val();
	var squareId = $('#square').val();
	$.ajax( {
		type : "GET",
		contentType : "application\json",
		dataType : "json",
		url : "updateMappingData",
		async : true,
		data:{
			currentPage:currentPage,
			objectType:objectType,
			parentType:parentType,
			parentNameId:parentNameId,
			state:state,
			type:type,
			innerName:encodeURI(innerName),
			squareId:squareId
		},
		success:function(msgs){
			$('#leftFY').show();
			$('#leftFY').val("update");
			$('#rightFY').show();
			$('#myTable tr:not(:first)').empty();
			var mappingDataList = msgs.mappingDataList;
			
			$('#curpage').html(msgs.currentPage);
			$('#allpage').html(msgs.maxPage);
			if (msgs.maxPage == 1) {
				$('#leftFY').hide();
				$('#rightFY').hide();
			}
			if (msgs.currentPage == 1) {
				$('#leftFY').hide();
			}
			if (msgs.currentPage == msgs.maxPage) {
				$('#rightFY').hide();
			}
			if(mappingDataList.length==0){
				$('#divfanye').hide();
			}else{
				$('#divfanye').show();
			}
			// 获取语言包
			var language = msgs.language;
			// 获取当前登录用户名
			var recentUserName = msgs.recentUserName;
			if(mappingDataList.length==0){
				$('#myTable')
				.append("<tr><td class='xytd' colspan=10>没有您要查询的数据！</tr>");
			}else{
				for(var i=0;i<mappingDataList.length;i++){

					var tempMappingData = mappingDataList[i];
					var id = tempMappingData.id;
					var innerId = tempMappingData.innerid;
					var innerName = tempMappingData.innername;
					var outId = tempMappingData.outid;
					var outName = tempMappingData.outname;
					var state = tempMappingData.state;
					var operation = tempMappingData.operation;
					var objectType = tempMappingData.objectType;
					
					var hiddenOperation = "<input type='hidden' value='"+operation+"'/>";
					var hiddenObjectType = "<input type='hidden' value='"+objectType+"'/>";
					
					var checkbox = "";
					if(state == "1"){
						checkbox = "<input type='checkbox' class='checkState' style='margin-right:0.2em' checked='checked'>使能</input>";
						outN = outName;
						outI = outId;
					}else{
						checkbox = "<input type='checkbox' class='checkState' style='margin-right:0.2em'>使能</input>"
						outN = "<input type='text'  style='width:12em' value='"+outName+"'></input>";
						outI = "<input type='text'  style='width:12em' value='"+outId+"'></input>";
					}
					
					var save = "<a href='#' class='fontOrange saveClass' style='color:#ee7800 !import'" 
								+ "'<span>"+"添加"+"</span> </a>";
					
					var del = "<a href='#' class='fontOrange delClass' style='color:#ee7800 !import'" 
						+ "'<span>"+"删除"+"</span> </a>";
					
					var op;
					if("insert"==operation){
						op=save;
					}else{
						op=del;
					}
					
					$('#myTable')
							.append(
									"<tr>"
											+ "<td style='display:none' class='mappingDataId'>"
											+ id
											+ "</td><td>"
											+ innerName
											+ "</td><td>"
											+ innerId
											+ "</td><td>"
											+ outN
											+ "</td><td>"
											+ outI
											+ "</td><td style='display:none;width:0px'>"
											+ hiddenOperation
											+ "</td><td>"
											+ checkbox
											+ "</td><td style='display:none;width:0px'>"
											+ hiddenObjectType
											+ "</td><td><div class='editdiv' value='"+tempMappingData.id+"'>"
											+ op
											+ "</div></td></tr>");
				
				}
			}
			hideLoading();
			addClickEventForSave();
			addClickEventForCheckBox();
		}
	} );
}


function updateNameCombo(){
	var objectType = $('#objectType').val();
	var parentType = $('#parentType').val();
	var parentNameId = $('#parentName').val();
	var squareId = $('#square').val();
	$.ajax( {
		type : "GET",
		contentType : "application\json",
		dataType : "json",
		url : "updateNameCombo",
		async : true,
		data:{
			objectType:objectType,
			parentType:parentType,
			parentNameId:parentNameId,
			squareId:squareId
		},
		success:function(msgs){
			var siteNameList = msgs.siteNameList;
			$('#parentName option').remove();
			if(siteNameList.length == 0){
				var option = "<option>暂无数据</option>";
				$('#parentName').append(option);
			}
			for(var i=0;i<siteNameList.length;i++){
				var temp = siteNameList[i];
				var option = "<option value='"+temp.innerid+"'>"+temp.innername+"</option>";
				$('#parentName').append(option);
			}
			
			updateMappingData();
		}
	} );
}

function updateCombo(){
	$('#gotoPage').val('1');
	search();
	var squareId = $('#square').val();
	var objectType = $('#objectType').val();
	
	
	$.ajax({
		type:'GET',
		contentType: 'application\json',
		dataType: 'json',
		data:{
			squareId: squareId,
			objectType: objectType
		},
		url: 'ajaxUpdateCombo',
		async: true,
		success:function(msgs){
			var siteTypeList = msgs.siteTypeList;
			$('#parentType option').remove();
			var siteNameList = msgs.siteNameList;
			$('#parentName option').remove();
			if(siteTypeList.length == 0){
				var option = "<option>暂无数据</option>";
				$('#parentType').append(option);
				var option = "<option>暂无数据</option>";
				$('#parentName').append(option);
			}
			for(var i=0;i<siteTypeList.length;i++){
				var temp = siteTypeList[i];
				var option = "<option value='"+temp.innerid+"'>"+temp.innername+"</option>";
				$('#parentType').append(option);
			}
			for(var i=0;i<siteNameList.length;i++){
				var temp = siteNameList[i];
				var option = "<option value='"+temp.innerid+"'>"+temp.innername+"</option>";
				$('#parentName').append(option);
			}
		}
	})
}

function addClickEventForCheckBox(){
	$('.checkState').each(function(){
		$(this).click(function(){
			makeOne(this);
		})
	})
}

function addClickEventForSave(){
	var square = $('#square').val();
	$('.saveClass').each(function(){
		$(this).click(function(){
			var $temp = $(this).parent();
			while(!$temp.is('tr')){
				$temp = $temp.parent();
			}
			var $tr = $temp;
			var type = $('#type').val();
			var array = $tr.children();
			var id = $(array[0]).text()
			var innerId = $(array[2]).text();
			var innerName = $(array[1]).text();
			var outId = $($(array[4]).children()[0]).val();
			var outName = $($(array[3]).children()[0]).val();
			var operation = $($(array[5]).children()[0]).val();
			var state = $(array[6]).children()[0].checked;
			var objectType = $($(array[7]).children()[0]).val();
			state = state ? 1 : 0;
			$.ajax({
				url:"saveOrUpdate",
				type:"post",
				dataType:"json",
				async:true,
		        data: { //发送给数据库的数据
					id : id,
				 	type : type,
				 	innerId : innerId,
				 	innerName : innerName,
				 	outId : outId,
				 	outName : outName,
				 	operation : operation,
				 	objectType : objectType,
				 	state : state,
				 	square : square
				 },
				success:function(msg){
					 alert("添加数据成功");
					 var arr = $temp.children();
					 var editDiv = $(arr[arr.length-1]).children()[0];
					 var operatorTd = $(arr[5]);
					 var input = $(operatorTd).children()[0];
					 if($(input).is('input')){
						 $(input).val('update');
					 }
					 $(editDiv).children().each(function(){
						$(this).remove(); 
					 });
					 var del = "<a href='#' class='fontOrange delClass' style='color:#ee7800 !import'" 
							+ "'<span>"+"删除"+"</span> </a>";
					 
					 $(editDiv).append(del);
					 
					 var infoId = msg.infoId;
					 if(infoId!=undefined){
						 var idTd = arr[0];
						 $(idTd).text(infoId);
					 }
					 
					 addClickEventForDel();
					 updateCheckPage();
				 },
				error: function (XMLHttpRequest, textStatus, thrownError) {
					 alert("添加数据失败")
				 }
			
			});
		});
	});
}

function back(){
	window.location.href = "dataTransition";
}

function addAllMappingData(){
	showLoading();
	var square = $('#square').val();
	var type = $('#type').val();
	$.ajax({
		url:"addAllMappingData",
		type:"post",
		dataType:"json",
		async:true,
        data: { //发送给数据库的数据
		 	square : square,
		 	type : type
		 },
		success:function(msg){
			 alert("保存映射成功");
			 search();
			 
		 },
		error: function (XMLHttpRequest, textStatus, thrownError) {
			 flag = false;
		 }
	
	});
}

function addClickEventForDel(){
	var square = $('#square').val();
	$('.delClass').each(function(){
		$(this).click(function(){
			var $temp = $(this).parent();
			while(!$temp.is('tr')){
				$temp = $temp.parent();
			}
			var $tr = $temp;
			var array = $tr.children();
			var id = $(array[0]).text()
			$.ajax({
				url:"deleteMappingData",
				type:"post",
				dataType:"json",
				async:true,
		        data: { //发送给数据库的数据
					id : id
				 },
				success:function(msg){
					 alert("删除映射成功");
					 var arr = $temp.children();
					 var editDiv = $(arr[arr.length-1]).children()[0];
					 $(editDiv).children().each(function(){
						$(this).remove(); 
					 });
					 
					 var innerIdTd = arr[2];
					 var innerNameTd = arr[1];
					 var checkBoxTd = arr[6];
					 var outIdTd = arr[4];
					 var outNameTd = arr[3];
					 var operatorTd = arr[5];
					 
					 var innerId = $(innerIdTd).text();
					 var innerName = $(innerNameTd).text();
					 
					 var outN = "<input type='text'  style='width:12em' value='"+innerName+"'></input>";
					 var outI = "<input type='text'  style='width:12em' value='"+innerId+"'></input>";
					 
					 var operatorInput = $(operatorTd).children()[0];
					 $(operatorInput).val("insert");
					 $(outIdTd).children().each(function(){
						 $(this).remove();
					 });
					 $(outIdTd).text("").append(outI);
					 $(outNameTd).children().each(function(){
						$(this).remove(); 
					 });
					 $(outNameTd).text("").append(outN);
					 
					 var checkb = $(checkBoxTd).children()[0];
					 $(checkb).attr('checked','');
					 
					 var save = "<a href='#' class='fontOrange saveClass' style='color:#ee7800 !import'" 
							+ "'<span>"+"添加"+"</span> </a>";
					 
					 $(editDiv).append(save);
					 
					 addClickEventForSave();
					 updateCheckPage();
				 },
				error: function (XMLHttpRequest, textStatus, thrownError) {
					 alert("添加数据失败")
				 }
			
			});
		});
	});

}

function changeState(){
	$('#gotoPage').val(1);
	updateMappingData();
}

function updateCheckPage(){
	var flag = true;
	$('.checkState').each(function(){
		var state = $(this).attr("checked");
		if(!state){
			flag = false;
		}
	});
	if(flag){
		$('#checkPage').attr('checked',"checked");
	}else{
		$('#checkPage').attr('checked','');
	}
}
